package com.tianji.api.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.tianji.api.cache.UserCache;
import com.tianji.api.client.user.UserClient;
import com.tianji.api.dto.user.UserDTO;
import org.springframework.context.annotation.Bean;

import java.time.Duration;
import java.util.Map;

public class UserCacheConfig {
    /**
     * todo 用户的caffeine缓存 - 注入到spring容器中
     */
    @Bean
    public Cache<String, Map<Long, UserDTO>> UserCaches(){
        return Caffeine.newBuilder()
                .initialCapacity(1) // 容量限制
                .maximumSize(10_00) // 最大内存限制
                .expireAfterWrite(Duration.ofMinutes(30)) // 有效期
                .build();
    }
    /**
     * todo 用户的缓存工具类 - 注入到spring容器中
     */
    @Bean
    public UserCache UserCache(
            Cache<String, Map<Long, UserDTO>> userCaches, UserClient userClient){
        return new UserCache(userCaches, userClient);
    }
}
